<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 寻找身高相近的小朋友（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>小明今年升学到了小学1年级来到新班级后&#xff0c;发现其他小朋友身高参差不齐&#xff0c;然后就想基于各小朋友和自己的身高差&#xff0c;对他们进行排序&#xff0c;请帮他实现排序。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行为正整数 h和n&#xff0c;0&lt;h&lt;200 为小明的身高&#xff0c;0&lt;n&lt;50 为新班级其他小朋友个数。</p> 
<p>第二行为n个正整数&#xff0c;h1 ~ hn分别是其他小朋友的身高&#xff0c;取值范围0&lt;hi&lt;200&#xff0c;且n个正整数各不相同。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出排序结果&#xff0c;各正整数以<strong>空格</strong>分割&#xff0c;</p> 
<p>和小明身高差绝对值最小的小朋友排在前面&#xff0c;</p> 
<p>和小明身高差绝对值最大的小朋友排在后面&#xff0c;</p> 
<p>如果两个小朋友和小明身高差一样&#xff0c;则个子较小的小朋友排在前面。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">100 10<br /> 95 96 97 98 99 101 102 103 104 105</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">99 101 98 102 97 103 96 104 95 105</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">小明身高100&#xff0c;班级学生10个&#xff0c;身高分别为95 96 97 98 99 101 102 103 104 105&#xff0c;按身高差排序后结果为&#xff1a;99 101 98 102 97 103 96 104 95 105。</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>考察多条件排序。</p> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;
import java.util.StringJoiner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int h &#61; sc.nextInt();
    int n &#61; sc.nextInt();

    Integer[] heights &#61; new Integer[n];
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) heights[i] &#61; sc.nextInt();

    System.out.println(getResult(h, heights));
  }

  public static String getResult(int h, Integer[] heights) {
    Arrays.sort(
        heights,
        (a, b) -&gt; {
          int absA &#61; Math.abs(a - h);
          int abaB &#61; Math.abs(b - h);

          if (absA !&#61; abaB) return absA - abaB;
          else return a - b;
        });

    StringJoiner sj &#61; new StringJoiner(&#34; &#34;);
    for (Integer height : heights) {
      sj.add(height &#43; &#34;&#34;);
    }
    return sj.toString();
  }
}
</code></pre> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    const [h, n] &#61; lines[0].split(&#34; &#34;).map(Number);
    const heights &#61; lines[1].split(&#34; &#34;).map(Number);

    console.log(getResult(h, heights));

    lines.length &#61; 0;
  }
});

function getResult(h, heights) {
  heights.sort((a, b) &#61;&gt; {
    const absA &#61; Math.abs(a - h);
    const absB &#61; Math.abs(b - h);

    if (absA !&#61; absB) return absA - absB;
    else return a - b;
  });

  return heights.join(&#34; &#34;);
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
h, n &#61; map(int, input().split())
heights &#61; list(map(int, input().split()))


# 算法入口
def getResult():
    heights.sort(key&#61;lambda x: (abs(x-h), x))
    return &#34; &#34;.join(map(str, heights))


# 调用算法
print(getResult())
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>